package com.example.day08upload;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;




public class CircleProgressBar extends View {
    private float mSweepAngle;
    String TAG = "CircleProgressBar";

    private int mRingColor;
    private float mRingWidth;
    private int mCircleColor;
    private float mStartAngle;
    private String mText;
    private float mTextSize;
    private int mTextColor;
    private RectF mRectF;
    private final Paint mArcPaint;
    private int mCx;
    private int mCy;
    private int mRadius;
    private final Paint mCirclePaint;
    private final Paint mTextPaint;
    private final float mDy;
    public CircleProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressBar);
        if (ta != null){
            mRingColor = ta.getColor(R.styleable.CircleProgressBar_ring_color, 0);
            mRingWidth = ta.getDimension(R.styleable.CircleProgressBar_ring_width, 20);
            mCircleColor = ta.getColor(R.styleable.CircleProgressBar_circle_color, 0);
            mStartAngle = ta.getFloat(R.styleable.CircleProgressBar_start_angle, -90);
            mSweepAngle = ta.getFloat(R.styleable.CircleProgressBar_sweep_angle, 0);
            mText = ta.getString(R.styleable.CircleProgressBar_android_text);
            mTextSize = ta.getDimension(R.styleable.CircleProgressBar_android_textSize, 30);
            mTextColor = ta.getColor(R.styleable.CircleProgressBar_android_textColor, 0);
            ta.recycle();
        }

        mArcPaint = new Paint();
        mArcPaint.setAntiAlias(true);
        mArcPaint.setStrokeWidth(mRingWidth);
        mArcPaint.setColor(mRingColor);
        mArcPaint.setStyle(Paint.Style.STROKE);//空心画笔

        //圆的画笔
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(mCircleColor);

        //文字画笔
        mTextPaint = new Paint();
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextSize(mTextSize);
        //水平居中
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        mDy = (float) ((fontMetrics.descent-fontMetrics.ascent)*0.5 - fontMetrics.descent);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int max = Math.max(width, height);
        int spec = MeasureSpec.makeMeasureSpec(max, MeasureSpec.EXACTLY);
        super.onMeasure(spec, spec);
        mRectF = new RectF(0.1f*max, 0.1f*max, 0.9f*max, 0.9f*max);
        //圆心
        mCx = max/2;
        mCy = max/2;
        //半径
        mRadius = max/4;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        //1.画环
        canvas.drawArc(mRectF,mStartAngle,mSweepAngle,false,mArcPaint);
        //2.画圆
        canvas.drawCircle(mCx,mCy,mRadius,mCirclePaint);
        //3.画文本
        canvas.drawText(mText,mCx,mCy+mDy,mTextPaint);
    }

    /**
     *
     * @param progress 0 -100
     */
    public void setProgress(int progress) {
        mText = progress+" %";
        mSweepAngle = progress*3.6f;
        postInvalidate();
    }
}